#include "reactor.hpp"
#include "socket.hpp"
#include "appInterface.hpp"
#include "util.hpp"
#include <string>
static void Usage(std::string proc) {
    std::cerr << "Usage :" << "\n\t" << proc << " port " << std::endl;
}

int main(int argc, char* argv[]) {
    if (argc != 2) {
        Usage(argv[0]);
        exit(4);
    }

    //套接字相关
    int port = atoi(argv[1]);
    int listen_sock = ns_sock::Sock::Socket();

    //设置listen_sock 为非阻塞状态
    ns_until::SetNoBlock(listen_sock);

    ns_sock::Sock::Bind(listen_sock, port);
    ns_sock::Sock::Listen(listen_sock, BACKLOG);

    //事件管理器相关
    ns_epoll::Reactor reactor;
    reactor.InitReactor();

    ns_epoll::EventItem item;
    item.sock = listen_sock;
    item.R = &reactor;



    item.ManagerCallBack(ns_appinterface::accepter, nullptr, nullptr);

    //将listen_sock托管给reactor//使用ET模式
    reactor.AddEvent(listen_sock, EPOLLIN|EPOLLET, item);

    int timeout = 1000;
    while (true) {
        reactor.Distatch(timeout);
    }
}
